import * as jwt from 'jsonwebtoken';
import userInfoModel from './../model/userInfoModel';


export class LoginUtils {

    /**
    * 解析token
    * @param authorization
    */
    decode(authorization) {
        // let token = authorization.substr(7);
        let auth = jwt.decode(authorization);

        return auth;
    }

    /**
     * 判断authorization 中的userToken 与数据库中的是否匹配
     */
    async checkUser(authorization) {
        let user: any = await userInfoModel.find({ where: { userToken: authorization } });
        let num = 100;
        if (user == null) {
            // throw new Error("你的用户已在其他设备登录,请勿重复登录");
            num = 101;
        }
        return num;
    }

}

let loginUtils = new LoginUtils();
export default loginUtils;